package me.zhengjie.modules.customer.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.customer.domain.TCustomerUser;
import me.zhengjie.modules.customer.service.TCustomerUserService;
import me.zhengjie.modules.customer.service.dto.TCustomerUserQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author xuyukun
* @date 2019-07-21
*/
@RestController
@RequestMapping("api")
public class TCustomerUserController {

    @Autowired
    private TCustomerUserService tCustomerUserService;

    @Log("查询TCustomerUser")
    @GetMapping(value = "/tCustomerUser")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERUSER_ALL','TCUSTOMERUSER_SELECT')")
    public ResponseEntity getTCustomerUsers(TCustomerUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tCustomerUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TCustomerUser")
    @PostMapping(value = "/tCustomerUser")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERUSER_ALL','TCUSTOMERUSER_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TCustomerUser resources){
        return new ResponseEntity(tCustomerUserService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TCustomerUser")
    @PutMapping(value = "/tCustomerUser")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERUSER_ALL','TCUSTOMERUSER_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TCustomerUser resources){
        tCustomerUserService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TCustomerUser")
    @DeleteMapping(value = "/tCustomerUser/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERUSER_ALL','TCUSTOMERUSER_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        tCustomerUserService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}